class Game {
    constructor() {
        this.canvas = document.getElementById('gameCanvas');
        this.ctx = this.canvas.getContext('2d');
        this.blockSize = 32;
        this.worldWidth = 25;
        this.worldHeight = 18;
        this.world = [];
        this.selectedBlock = 'grass';
        this.time = 0;
        this.dayDuration = 1000;
        
        this.blockTypes = {
            grass: { color: '#4CAF50', emoji: '🌱' },
            dirt: { color: '#8B4513', emoji: '🟫' },
            stone: { color: '#696969', emoji: '🪨' },
            wood: { color: '#8B4513', emoji: '🪵' },
            air: { color: 'transparent', emoji: '' }
        };
        
        this.init();
    }
    
    init() {
        this.generateWorld();
        this.setupEventListeners();
        this.gameLoop();
    }
    
    generateWorld() {
        this.world = [];
        for (let y = 0; y < this.worldHeight; y++) {
            this.world[y] = [];
            for (let x = 0; x < this.worldWidth; x++) {
                if (y > this.worldHeight - 3) {
                    this.world[y][x] = 'stone';
                } else if (y > this.worldHeight - 5) {
                    this.world[y][x] = 'dirt';
                } else if (y === this.worldHeight - 5) {
                    this.world[y][x] = 'grass';
                } else {
                    this.world[y][x] = 'air';
                }
                
                if (Math.random() < 0.1 && y > 0 && this.world[y-1][x] === 'air') {
                    this.world[y][x] = 'wood';
                }
            }
        }
    }
    
    setupEventListeners() {
        this.canvas.addEventListener('click', (e) => this.handleClick(e));
        this.canvas.addEventListener('contextmenu', (e) => {
            e.preventDefault();
            this.handleRightClick(e);
        });
        
        document.querySelectorAll('.block-btn').forEach(btn => {
            btn.addEventListener('click', (e) => {
                document.querySelectorAll('.block-btn').forEach(b => b.classList.remove('active'));
                e.target.classList.add('active');
                this.selectedBlock = e.target.dataset.block;
            });
        });
    }
    
    getMousePos(e) {
        const rect = this.canvas.getBoundingClientRect();
        return {
            x: Math.floor((e.clientX - rect.left) / this.blockSize),
            y: Math.floor((e.clientY - rect.top) / this.blockSize)
        };
    }
    
    handleClick(e) {
        const pos = this.getMousePos(e);
        if (this.isValidPos(pos.x, pos.y)) {
            this.world[pos.y][pos.x] = 'air';
        }
    }
    
    handleRightClick(e) {
        const pos = this.getMousePos(e);
        if (this.isValidPos(pos.x, pos.y)) {
            this.world[pos.y][pos.x] = this.selectedBlock;
        }
    }
    
    isValidPos(x, y) {
        return x >= 0 && x < this.worldWidth && y >= 0 && y < this.worldHeight;
    }
    
    updateTime() {
        this.time = (this.time + 1) % this.dayDuration;
        const timeRatio = this.time / this.dayDuration;
        const timeDisplay = document.getElementById('time-display');
        
        if (timeRatio < 0.25 || timeRatio > 0.75) {
            timeDisplay.textContent = '夜晚';
            document.body.style.background = 'linear-gradient(135deg, #191970, #000080)';
        } else {
            timeDisplay.textContent = '白天';
            document.body.style.background = 'linear-gradient(135deg, #87CEEB, #98FB98)';
        }
    }
    
    render() {
        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
        
        for (let y = 0; y < this.worldHeight; y++) {
            for (let x = 0; x < this.worldWidth; x++) {
                const blockType = this.world[y][x];
                if (blockType !== 'air') {
                    const block = this.blockTypes[blockType];
                    
                    this.ctx.fillStyle = block.color;
                    this.ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
                    
                    this.ctx.strokeStyle = '#333';
                    this.ctx.strokeRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
                    
                    this.ctx.font = '20px Arial';
                    this.ctx.textAlign = 'center';
                    this.ctx.textBaseline = 'middle';
                    this.ctx.fillText(
                        block.emoji,
                        x * this.blockSize + this.blockSize / 2,
                        y * this.blockSize + this.blockSize / 2
                    );
                }
            }
        }
        
        this.ctx.strokeStyle = '#333';
        this.ctx.lineWidth = 2;
        for (let x = 0; x <= this.worldWidth; x++) {
            this.ctx.beginPath();
            this.ctx.moveTo(x * this.blockSize, 0);
            this.ctx.lineTo(x * this.blockSize, this.worldHeight * this.blockSize);
            this.ctx.stroke();
        }
        for (let y = 0; y <= this.worldHeight; y++) {
            this.ctx.beginPath();
            this.ctx.moveTo(0, y * this.blockSize);
            this.ctx.lineTo(this.worldWidth * this.blockSize, y * this.blockSize);
            this.ctx.stroke();
        }
    }
    
    gameLoop() {
        this.updateTime();
        this.render();
        requestAnimationFrame(() => this.gameLoop());
    }
}

window.addEventListener('load', () => {
    new Game();
});
